#import memcache as memcache

from web.session import Store

'''
class MemcacheStore(Store):
    def __init__(self, config):
        3'
        config = {
        'servers': ['127.0.0.1:11211'],
        'timeout': 1440
        }
        3'
        self.mc = memcache.Client(config['servers'])
        self.timeout = config['timeout']

    def __contains__(self, key):
        return True if self.mc.get(key) else False

    def __getitem__(self, key):
        return self.mc.get(key)

    def __setitem__(self, key, value):
        self.mc.set(key, value, self.timeout)

    def __delitem__(self, key):
        self.mc.delete(key)

    def cleanup(self, timeout):
        3'You need nothing to do. Memcache can handle it.3'
        pass
'''

class MemStore(Store):
    def __init__(self, memcache):
        self.mc = memcache

    def __contains__(self, key):
        data = self.mc.get(key)
        return bool(data)

    def __getitem__(self, key):
        now = time.time()
        value = self.mc.get(key)
        if not value:
            raise KeyError
        else:
            value['attime'] = now
            self.mc.set(key,value)
            return value

    def __setitem__(self, key, value):
        now = time.time()
        value['attime'] = now
        s = self.mc.get(key)
        self.mc.set(key, value, web.config.session_parameters['timeout'])

    def __delitem__(self, key):
        self.mc.delete(key)

    def cleanup(self, timeout):
        pass

'''
class Store:
    """Base class for session stores"""

    def __contains__(self, key):
        raise NotImplementedError

    def __getitem__(self, key):
        raise NotImplementedError

    def __setitem__(self, key, value):
        raise NotImplementedError

    def cleanup(self, timeout):
        """removes all the expired sessions"""
        raise NotImplementedError

    def encode(self, session_dict):
        """encodes session dict as a string"""
        pickled = pickle.dumps(session_dict)
        return base64.encodestring(pickled)

    def decode(self, session_data):
        """decodes the data to get back the session dict """
        pickled = base64.decodestring(session_data)
        return pickle.loads(pickled)
'''
